//
//  GKZYExpertLineListController.m
//  GKZYAid
//
//  Created by lanou3g on 16/3/5.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYExpertLineListController.h"
#import "GKZYExpertLineModel.h"
#import "GKZYExpertLineCell.h"
#import "GKZYExpertLineDetailController.h"
#import "GKZYChatListController.h"
#import "GKZYRegisteredController.h"
#import "DatabaseRongcloud.h"
#import "GKZYChatDetailController.h"

#define kData [DatabaseRongcloud shareIntance]

@interface GKZYExpertLineListController ()<UITableViewDataSource,UITableViewDelegate>

//存放所有专家的数据
@property (nonatomic,strong)NSMutableArray *expertLineDataArray;

@end

@implementation GKZYExpertLineListController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    if([kData.userID isEqualToString:@"0"]||[kData.userID isEqualToString:@"1"]||[kData.userID isEqualToString:@"2"]||[kData.userID isEqualToString:@"3"]||[kData.userID isEqualToString:@"4"]||[kData.userID isEqualToString:@"5"])
    {
        kData.block();
    }
}

- (void)loadView{
    
    self.expertlineView = [[GKZYExpertLineView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.expertlineView;
    
}

//懒加载初始化:
- (NSMutableArray *)expertLineDataArray{
    
    if (!_expertLineDataArray) {
        
        _expertLineDataArray = [[NSMutableArray alloc]init];
        
    }
    return _expertLineDataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册cell:
    [self.expertlineView.tableView registerClass:[GKZYExpertLineCell class] forCellReuseIdentifier:@"expertLineCELL"];

    //设置代理:
    self.expertlineView.tableView.dataSource = self;
    self.expertlineView.tableView.delegate = self;
    
    //请求数据:
    [self makeData];
    


}

- (void)makeData{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Teacher" ofType:@"plist"];
    NSMutableArray *arrayData = [NSMutableArray arrayWithContentsOfFile:path];
    
    for (NSDictionary *dict in arrayData) {
        
        GKZYExpertLineModel *model = [GKZYExpertLineModel new];
        
        [model setValuesForKeysWithDictionary:dict];
        
        [self.expertLineDataArray addObject:model];
    }
    
       
}


#pragma mark------------实现UITableView代理方法-------------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.expertLineDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GKZYExpertLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expertLineCELL"];
    cell.model = self.expertLineDataArray[indexPath.row];
    cell.button.tag = 300 + indexPath.row;
    [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)buttonAction:(UIButton *)sender
{
    if([DatabaseRongcloud shareIntance].userID.length == 0)
    {
        GKZYRegisteredController *registerVC = [[GKZYRegisteredController alloc]init];
        [self.navigationController pushViewController:registerVC animated:YES];
    }
    else
    {
        NSString *userId = nil;
        NSString *userName = nil;
        if(sender.tag == 300)
        {
            userId = @"0";
            userName = @"王海洋";
        }
        else if(sender.tag == 301)
        {
            userId = @"1";
            userName = @"李海军";
        }
        else if(sender.tag == 302)
        {
            userId = @"2";
            userName = @"欧娇娇";
        }
        else if(sender.tag == 303)
        {
            userId = @"3";
            userName = @"马文龙";
        }
        else if(sender.tag == 304)
        {
            userId = @"4";
            userName = @"佟玉芬";
        }
        else if(sender.tag == 305)
        {
            userId = @"5";
            userName = @"马全";
            
        }
        
        
        GKZYChatDetailController *chatDC = [[GKZYChatDetailController alloc] initWithConversationType:ConversationType_PRIVATE targetId:userId];
        chatDC.titleName = userName;
        [self.navigationController pushViewController:chatDC animated:YES];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200;
}

//点击cell跳转事件:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    GKZYExpertLineDetailController *expertDetailVC = [storyboard instantiateViewControllerWithIdentifier:@"expertLineDetail"];
    expertDetailVC.model = self.expertLineDataArray[indexPath.row];
    [self.navigationController pushViewController:expertDetailVC animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

- (CGFloat )tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 150;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
